#  1. build image:
#     docker build -t lq-sdk:latest .
#     docker build --progress=plain -t lq-sdk:latest .
#
#  2.  run docker image:
#
#     2.1 run with default user
#     docker run -it -v $(pwd):$(pwd) -v $(pwd)/toolchain:/opt/toolchain lq_sdk:latest /bin/bash
#
#     2.2 run with root user
#     docker run -u 0 -it -v $(pwd):$(pwd) -v $(pwd)/toolchain:/opt/toolchain lq_sdk:latest /bin/bash
#

FROM ubuntu:20.04
LABEL verdor=LqSilicon
LABEL name=lq_sdk
LABEL version=0.1
LABEL date='2024-03-04'
LABEL auther='liuping.ma@lqsilicon.com'

ENV USER=gitlab
ARG UID=6000

# config for default software install
ARG DEBIAN_FRONTEND=noninteractive
# install 32 bit lib for toolchain
RUN dpkg --add-architecture i386

# config ubuntu local apt source
RUN cp /etc/apt/sources.list /etc/apt/sources_bak.list \
    && sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list \
    && sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

# install software
RUN apt-get clean all && \
    apt-get update && \
    apt-get install -y --fix-broken --fix-missing --no-install-recommends \
    sudo vim wget curl git git-lfs openssh-client net-tools sed tzdata expect mtd-utils inetutils-ping locales device-tree-compiler \
    sed make binutils build-essential diffutils gcc g++ bash patch gzip bzip2 perl tar cpio unzip rsync file bc findutils \
    dosfstools mtools bison flex autoconf automake \
    libc6-dev-i386 libncurses5:i386 libssl-dev \
    python3 python3-pip python-is-python3 python3-dev\
    lib32z1 scons libncurses5-dev swig\
    kmod fakeroot pigz tree doxygen gawk pkg-config libyaml-dev libconfuse2 libconfuse-dev \
	git libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev ninja-build \
	libaio-dev libbluetooth-dev libcapstone-dev libbrlapi-dev libbz2-dev \
	libcap-ng-dev libcurl4-gnutls-dev libgtk-3-dev \
	libibverbs-dev libjpeg8-dev libncurses5-dev libnuma-dev \
	librbd-dev librdmacm-dev \
	libsasl2-dev libsdl2-dev libseccomp-dev libsnappy-dev libssh-dev \
	libvde-dev libvdeplug-dev libvte-2.91-dev libxen-dev liblzo2-dev \
	valgrind xfslibs-dev


# config pip local source
RUN echo \
    '[global]\n\
    timeout = 60\n\
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple\n\
    extra-index-url = https://mirrors.aliyun.com/pypi/simple/ https://mirrors.cloud.tencent.com/pypi/simple\n\
    '  > /etc/pip.conf; cat /etc/pip.conf

# install pip software
RUN python3 -m pip install -U pyyaml pycryptodome gmssl \
    numpy==1.19.5  protobuf==3.17.3 Pillow onnx==1.9.0 onnx-simplifier==0.3.6 onnxoptimizer==0.2.6 onnxruntime==1.8.0  cmake


# required libconfuse-dev
RUN wget https://github.com/pengutronix/genimage/releases/download/v16/genimage-16.tar.xz -O /tmp/genimage-16.tar.xz \
    && cd /tmp/; tar -xf genimage-16.tar.xz; cd genimage-16 \
    && ./configure \
    && make -j \
    && make install \
    && cd .. ; rm -rf genimage*


# default encode
RUN rm -rf /var/lib/apt/lists/* \
    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8

# default timezone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
    && dpkg-reconfigure --frontend noninteractive tzdata

# add docker user and disable default root user
RUN groupadd --gid ${UID} ${USER} \
    && adduser --home /home/${USER} --uid ${UID} --gid ${UID} --shell /bin/bash --disabled-password --gecos '' ${USER} \
    && adduser ${USER} sudo \
    && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# toolchain and work dir
RUN mkdir -p /opt/toolchain/

# switch to new user
USER ${USER}

# switch work dir
WORKDIR /home/${USER}/

